package com.invengo.testrfid.app.utils;


import com.invengo.testrfid.MyApplication;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

/**
 * Created by shengjunhao on 2017/7/22.
 */

public class WebServiceUtils {

    /**
     *
     * @param ContainerNo
     * @param RfidNo1
     * @param RfidNo2
     * @param BindingTime
     * @param calback
     */
    public static void zs(String ContainerNo, String RfidNo1, String RfidNo2, String BindingTime, MyCalback calback) {

        if (!WifeStatueUtils.checkNetworkState(MyApplication.getContext())) {
            return;
        }
        // 命名空间
        String nameSpace = "http://tempuri.org/";

        // 调用的方法名称
//        String methodName = "zs";
        String methodName = "UpdateContainerInfo2";

        // EndPoint
//        String endPoint = "http://www.imahi.cn/dsrb.asmx";
//        String endPoint = "http://192.170.0.115:6666/WebService1.asmx";// 测试环境
        String endPoint = "http://182.92.109.121:6768/WebService1.asmx";// 正式环境

        // SOAP Action
        String soapAction = "http://tempuri.org/UpdateContainerInfo2";

        // 指定WebService的命名空间和调用的方法名
        SoapObject rpc = new SoapObject(nameSpace, methodName);

        // 设置需调用WebService接口需要传入的两个参数mobileCode、userId
        rpc.addProperty("ContainerNo", ContainerNo);
        rpc.addProperty("RfidNo1", RfidNo1);
        rpc.addProperty("RfidNo2", RfidNo2);
        rpc.addProperty("RfidNo2", RfidNo2);
        rpc.addProperty("BindingTime", BindingTime);

        // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);

        envelope.bodyOut = rpc;
        // 设置是否调用的是dotNet开发的WebService
        envelope.dotNet = true;
        // 等价于envelope.bodyOut = rpc;
        envelope.setOutputSoapObject(rpc);

        HttpTransportSE transport = new HttpTransportSE(endPoint);
        try {
            // 调用WebService
            transport.call(soapAction, envelope);
        } catch (Exception e) {
            e.printStackTrace();
            calback.onError(e.toString());
            return;
        }

        if (envelope.bodyIn instanceof SoapFault) {
            String str = ((SoapFault) envelope.bodyIn).faultstring;
            MyLogUtils.log("tag", "err_" + str, 4);
            calback.onError(str);
        } else {

            // 获取返回的数据
            SoapObject object = (SoapObject) envelope.bodyIn;

            if(object == null){
                calback.onError("服务器返回数据为空");
                return;
            }
            // 获取返回的结果
            // String result = object.toString();
            String result = object.getProperty(0).toString();
            String[] split = result.split("[|][|][|]");
            if(split.length != 2){

                calback.onError("数据返回格式错误");
                return;
            }
            if(Integer.parseInt(split[0]) == 0){
                calback.onError(split[1]);
                return;
            }
            calback.onSuccesss(result);
        }
    }
}
